package com.struts2.action;

import java.util.Map;






import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.struts2.model.Designer;
import com.struts2.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class LoginAction extends ActionSupport implements SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// For SessionAware
	private Map<String, Object> session;
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	private String username;
	private String password;
	private String type;
	
	public void validate() {
		System.out.println("validate start");
		if (StringUtils.isEmpty(username)) {
			addFieldError("username", "Name cannot be blank");
		}
		if (StringUtils.isEmpty(password)) {
			addFieldError("password", "Password cannot be blank");
		}
    }
	
	public String login() {
		System.out.println("login called");
		System.out.println(username);
		System.out.println(password);
		System.out.println(type);
		LoginService loginService = new LoginService();
		System.out.println(loginService.verifyCustomerLogin(username, password));
		if (type.equals("Designer")){
			if (loginService.verifyLogin(username, password)) {
				session.put("login", username);
				session.put("loginID", password);
				
				return "DESIGNER_SUCCESS";
			}
		}else if (type.equals("Customer")){ 
			if (loginService.verifyCustomerLogin(username, password)){
				session.put("customerLogin", username);
				return "CUSTOMER_SUCCESS";
			}
		}
		addActionError("Username or Password is wrong!");
		return LOGIN;
	}
	
	@SkipValidation
	public String logout(){ 
		session.remove("login");
		session.remove("loginID");
		return SUCCESS;
	}
	
	@SkipValidation
	public String customerLogout(){ 
		session.remove("customerLogin");
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
